var currentDateField = null ;

// convert a string to a Date given dateFormat in the format MDY
function stringToDate(dateIn, dateFormat)
{
   if (dateFormat == null)
      alert('Unable to format date - no format filter was supplied.') ;

   switch (dateFormat){
   case 'DMY':
      var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})\2(\d{2}|\d{4})$/;
      break ;
   case 'MDY':
      var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})\2(\d{2}|\d{4})$/;
      break ;
   case 'YDM':
      var datePat = /^(\d{2}|\d{4})(\/|-|.)(\d{1,2})\2(\d{1,2})$/;
      break ;
   case 'YMD':
      var datePat = /^(\d{2}|\d{4})(\/|-|.)(\d{1,2})\2(\d{1,2})$/;
      break ;
   }

   var matchArray = dateIn.match(datePat); // is the format ok?
   if (matchArray == null) {
      return null ;

   }
   var month = 0 ;
   var day = 0 ;
   var year = 0 ;

   switch (dateFormat){
   case 'DMY':
      month = matchArray[3] ;
      day   = matchArray[1] ;
      year  = matchArray[4] ;
      break ;
   case 'MDY':
      month = matchArray[1] ;
      day   = matchArray[3] ;
      year  = matchArray[4] ;
      break ;
   case 'YDM':
      month = matchArray[4] ;
      day   = matchArray[3] ;
      year  = matchArray[1] ;
      break ;
   case 'YMD':
      month = matchArray[3] ;
      day   = matchArray[4] ;
      year  = matchArray[1] ;
      break ;
   }
   if (year.length == 2 && year < 30) 
      year = '20' + year ;

   return new Date(year, month - 1, day) ;

}



function validateDates(fromDateField, toDateField, dateFormat, msg) 
{
   msg = msg || 'Please enter a to date that is greater than the from date.' ;

   var fromDate = stringToDate(fromDateField.value.stringTrim(), dateFormat) ;
   var toDate = stringToDate(toDateField.value.stringTrim(), dateFormat) ;

   if (toDate == null || fromDate == null) {
      alert('Please enter a valid date.') ;
      if (toDate == null)
         toDateField.focus() ;
      else
         fromDateField.focus() ;

      return false ;
   }

   if (fromDate > toDate) {
      alert(msg) ;
      fromDateField.focus() ;
      return false ;
   }

   return true ;
}


//  given a date format, validate any number of dates passed in
function validateFormDateFields(dateFormat, canBePastDate)
{
   var fieldName = null, len = null ;
   var formElement = null, dateVal = null ;
   var badFieldList = new Array() ;
   
   var today = new Date ;
   today.setHours(0, 0, 0, 0) ;

   for (var i = 2; i < arguments.length; i++) {
      formElement = arguments[i] ;
   
      len = formElement.length ;
      
      if (len == null) {
         if ((dateVal = formElement.value.stringTrim()).length > 0)
            if ((dateVal = stringToDate(dateVal, dateFormat)) == null || 
                                 (!canBePastDate && dateVal < today )) 
               badFieldList[badFieldList.length] = formElement ;

      } else {
         for (var y = 0; y < len; y++)
            if ((dateVal = formElement[y].value.stringTrim()).length > 0)
               if ((dateVal = stringToDate(dateVal, dateFormat)) == null || 
                                 (!canBePastDate && dateVal < today )) 
                  badFieldList[badFieldList.length] = formElement ;
      } 
   }
   
   if (badFieldList.length > 0) {
      badFieldList[0].focus() ;
      var fieldList = "" ;
      
      for (var i = 0 ; i < badFieldList.length ; i++)
         fieldList += (badFieldList[i].friendlyName || badFieldList[i].name) + "\n";
      
      var errorMsg = 'Please enter a valid date for the following field(s): ' + "\n\n" + fieldList;
      errorMsg += canBePastDate ? "" : '\n' + 'Please make sure the date is not earlier than today.';
      
      alert(errorMsg);
      
      return false ;
   }

   return true ;
}


function validDateField(dateField, dateFormat,fieldName) 
{
  
   //var aDate = stringToDate(dateField.value.stringTrim(), dateFormat) ;
   var aDate = stringToDate(dateField.stringTrim(), dateFormat) ;

   if (aDate == null) {
      if (fieldName != null)
         alert('Please enter a valid date for the following field(s): ' + fieldName);
      else   
         alert('Please enter a valid date.');
         
      dateField.focus() ;

      return false ;
   }
   return true ;
}



function calPopup(url, yr, mn, dy, evnt)
{  
   var x = 0, y = 0 ;
   if (evnt) {
      x = evnt.screenX ;
      y = evnt.screenY ;
   }

   url += (url.indexOf("?") < 0)?'?':'&';

   url += 'y=' + yr + '&m=' + mn + '&d=' + dy ;
   var params = 'width=188,height=255,resizable=yes,scrollbars=no,status=0,screenX='+x+',screenY='+y+',left='+x+',top='+y ;
   
   var popup = window.open(url, 'EPTaskCalPopup', params);

   if (popup != null)
   {
      if (popup.opener == null)
         popup.opener = self ;

   }
}


//  call this with the form date field, the date format ('MDY', 'DMY', etc), and the event
function startDatePopup(dateField, dateFormat, evnt, url)
{
   // this will only work in IE
   // the event should be passed in to the function
   if (evnt == null) evnt = window.event ;  
   
   currentDateField = dateField ;
   theDate = stringToDate(dateField.value, dateFormat) ;

   if (theDate == null)
      theDate = new Date() ;

   var dy = theDate.getDate() ;
   var yr = theDate.getFullYear() ;
   var mn = theDate.getMonth() + 1 ;

   calPopup( url, yr, mn, dy, evnt);
}


function setDate(y, m, d, field, dateFormat, delimiter)
{

   if (currentDateField != null) {
      switch (dateFormat) {
      case 'MDY':
         currentDateField.value = m + delimiter + d + delimiter + y ;
         break ;

      case 'DMY':
         currentDateField.value = d + delimiter + m + delimiter + y ;
         break ;

      case 'YDM':
         currentDateField.value = y + delimiter + d + delimiter + m ;
         break ;

      case 'YMD':
         currentDateField.value = y + delimiter + m + delimiter + d ;
         break ;
      }
   }

}
